var cm_query_goaldetail = new Ext.grid.ColumnModel([
	new Ext.grid.CheckboxSelectionModel({singleSelect : false}),
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
	{id: 'categoryId',header: "指标目录", width: 80, hidden: true, dataIndex: 'categoryId'},
	{id: 'categoryNamePath',header: "指标目录路径", width: 180, sortable: true, dataIndex: 'categoryNamePath'},
	{id: 'content',header: "考核内容", width: 200, sortable: true, dataIndex: 'content'},
	{id: 'point',header: "标准分值", width: 60, sortable: true, dataIndex: 'point'},
	{id: 'spec',header: "评分标准", width: 150, sortable: true, dataIndex: 'spec'},
	{id: 'ordering',header: "排序", width: 100, hidden : true, dataIndex: 'ordering'},
	{id: 'memo',header: "备注", width: 100, hidden : true, dataIndex: 'memo'},
	{id: 'checkOrgId',header: "考核责任单位", width: 80, sortable: true, dataIndex: 'checkOrgId',
		renderer: function(value, p, r) {
			var ret = '';
			var size = ds_org_select.getCount();
			for (var i = 0; i < size; i++) {
				var record = ds_org_select.getAt(i);
				var id = record.get('id');
				if (id == value) {
					ret = record.get('name');
					break;
				}
			}
			return (ret == '' || ret == null) ? value : ret;
		}},
	{id: 'percent',header: "换算关系(%)", width: 80, sortable: true, dataIndex: 'percent'},
    {id: 'categoryIdPath',header: "指标目录id 路径 ", width: 80, hidden : true, dataIndex: 'categoryIdPath'},
	{id: 'year',header: "考核年度", width: 120,hidden : true, dataIndex: 'year'}
]);
cm_query_goaldetail.defaultSortable = false;
var btn_add_goal_setting = new Ext.Button({
	text : '加入',
	iconCls : 'icon-add',
	handler : function() {
		if(!grid_query_goaldetail.checkMany()){
			return;
		} else {
			Ext.Msg.confirm('确认提示', '你确定要加入这些考核指标?', function(btn) {
				if (btn == 'yes') {
				　 	var frm = query_goaldetail.findByType('form')[0].getForm();
					var orgId  = frm.findField('orgId').getValue();
					var records = grid_query_goaldetail.getSelectionModel().getSelections();
					var ids = [];
					for(var i = 0, count = records.length; i < count; i++){
						ids.push(records[i].get('id'));
					}
				//		提交保存数据
					Ext.getBody().mask("数据正在提交，请稍等...","x-mask-loading"); 
					Ext.Ajax.request({
						url : '/supervise/biz/orgcheck/saveOrgCheckSetting.action',
						success : function(response, options) {
							Ext.getBody().unmask(); 
							var store = grid_orgchecksetting.getStore();
						    var fromRecord = null;
							var toRecord = null;
							for(var i = 0, count = records.length; i < count; i++){
								toRecord = new OrgCheckSetting({
					//						id : action.result.id,
									detailId : records[i].get('id'),
					//						categoryId : records[i].get('categoryId'),
									content : records[i].get('content'),
									point : records[i].get('point'),
									spec : records[i].get('spec'),
									orgId : records[i].get('checkOrgId'),
									year : records[i].get('year'),
									type : 'normal'
								});
					//					alert(Ext.util.JSON.encode(toRecord));
								store.insert(0, [toRecord]);
							}
							window_query_goaldetail.hide();
						},
						failure : function() {
							Ext.getBody().unmask();
							Ext.Msg.alert('提示', '数据保存失败！');
						},
						params : {
							orgId : orgId,
							goalIds : ids.join(','),
							goalType : 'normal'
						}
					});
				}
			});
		}
	}
});
function fun_add_goal(){
	var frm = query_goaldetail.findByType('form')[0].getForm();
//		alert(frm);
	var orgId  = frm.findField('orgId').getValue();
//		alert(orgId);
	var records = grid_query_goaldetail.getSelectionModel().getSelections();
	var ids = [];
	for(var i = 0, count = records.length; i < count; i++){
		ids.push(records[i].get('id'));
	}
//		alert(ids);
//		提交保存数据
	Ext.getBody().mask("数据正在提交，请稍等...","x-mask-loading"); 
	Ext.Ajax.request({
		url : '/supervise/biz/orgcheck/saveOrgCheckSetting.action',
		success : function(response, options) {
			Ext.getBody().unmask();
			var store = grid_orgchecksetting.getStore();
		    var fromRecord = null;
			var toRecord = null;
			for(var i = 0, count = records.length; i < count; i++){
				toRecord = new OrgGoalSettingDto({
					detailId : records[i].get('id'),
	//						categoryId : records[i].get('categoryId'),
					content : records[i].get('content'),
					point : records[i].get('point'),
					spec : records[i].get('spec'),
					orgId : records[i].get('checkOrgId'),
					year : records[i].get('year'),
					goalType : 'normal'
				});
	//					alert(Ext.util.JSON.encode(toRecord));
//				store.insert(0, [toRecord]);
			}
			window_query_goaldetail.hide();
		},
		failure : function() {
			Ext.getBody().unmask();
			window_edit_goalcategory.hide();
			Ext.Msg.alert('提示', '数据保存失败！');
		},
		params : {
			orgId : orgId,
			goalIds : ids.join(','),
			goalType : 'normal'
		}
	});
}
var ds_query_goaldetail = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/goaldetail/queryGoalDetailByPage.action'
	}),
	reader : new Ext.data.JsonReader({
		root : 'root',
		totalProperty : 'totalProperty'
	}, [{
			name : 'id',
			type : 'int'
		}, {
			name : 'categoryId',
			type : 'int'
		},{
			name : 'content',
			type : 'string'
		},{
			name : 'point',
			type : 'float'
		},{
			name : 'spec',
			type : 'string'
		}, {
			name : 'ordering',
			type : 'float'
		}, {
			name : 'memo',
			type : 'string'
		},{
			name : 'checkOrgId',
			type : 'int'
		},{
			name : 'percent',
			type : 'float'
		},{
			name : 'categoryIdPath',
			type : 'string'
		},{
			name : 'categoryNamePath',
			type : 'string'
		},{
		name : 'year',
		type : 'int'
	}])
});

var grid_query_goaldetail = new Ext.grid.GridPanel({
	iconCls : 'icon-grid',
	loadMask : {
		msg : '数据加载中...'
	},
	height : 360,
	region : 'center',
	cm : cm_query_goaldetail,
	sm : new Ext.grid.CheckboxSelectionModel({
		singleSelect : false
	}),
	store : ds_query_goaldetail,
	tbar : [btn_add_goal_setting],
	bbar : new Ext.PagingToolbar({
		pageSize : 20,
		store : ds_query_goaldetail,
		displayInfo : true,
		displayMsg : '第 {0} - {1} 条  共 {2} 条',
		refreshText : "刷新",
		emptyMsg : "没有记录"
	}),
	// 检测至少选择一个
	checkOne : function() {
		var selections = this.getSelectionModel().getSelections();
		if (selections.length == 0) {
			Ext.MessageBox.alert("提示", "请选择一条的记录！");
			return false;
		} else if (selections.length != 1) {
			Ext.MessageBox.alert("提示", "不能选择多行！");
			return false;
		}
		return true;
	},

	// 检测必须选择一个
	checkMany : function() {
		var selections = this.getSelectionModel().getSelections();
		if (selections.length == 0) {
			Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
			return false;
		}
		return true;
	}
});
var query_goaldetail = new Ext.Panel({
	height : 175,
	region : "north",
	baseCls:"x-plain",
	style:"padding:5px",
	layout:"column",
	items:[{
		columnWidth:0.8,
		layout:"form",
		frame : true,
		baseCls : 'x-plain',
		items : [new Ext.FormPanel({
			id: 'form_query_goaldetail_add',
			labelAlign : 'right',
			border : false,
			baseCls : 'x-plain',
			bodyStyle : 'padding:5px 5px 0',
			defaults : {
				anchor : '90%',
				width : 240,
				msgTarget : 'side' // 验证信息显示右边
			},
			defaultType : 'textfield',
			items:[{ 
					fieldLabel : '考核年度',
					name : 'yearName' , 
					disabled : true
				},{
					xtype : 'hidden',
					name : 'year'
				}, {
					xtype : 'hidden',
					name : 'categoryId'
				}, {
					fieldLabel : '部门',
					name : 'orgName',
					disabled : true
				},{
					id : 'combotree_query_goal_add',
					xtype : 'combotree',
					fieldLabel : '指标目录',
					hiddenName : 'goalDetail.categoryId',
					selectNodeModel:'exceptRoot', //只有选叶子时，才设置值  
					allowBlank : true,
					height : 120,
					tree : {
						xtype:'treepanel',
						rootVisible : false,
						autoScroll : true,
						firstLoad : true,
						loadingText : '加载中...',
						loader : new Ext.tree.TreeLoader(),
						root : new Ext.tree.AsyncTreeNode(),
						listeners : {
							'beforeload' : function(node) {
									var frm = Ext.getCmp("form_query_goaldetail_add").getForm();
									var checkYear = frm.findField('year').getValue();
									
									var nodeId = Ext.num(node.id, null);
									if (Ext.isNumber(nodeId)) {
										this.loader.dataUrl = '/supervise/biz/goalcategory/getChildrenById.action?checkYear='
												+ checkYear + '&nodeId=' + node.id;
									} else {
										this.loader.dataUrl = "/supervise/biz/goalcategory/getRoot.action?checkYear=" + checkYear;
									}
								}
							}
				    	},
				    	listeners:{  
				            beforeselect: function(comboxtree,newNode,oldNode){//选择树结点设值之前的事件  
			            },  
			            select: function(comboxtree,newNode,oldNode){//选择树结点设值之后的事件  
			            	var frm = Ext.getCmp("form_query_goaldetail_add").getForm();
			            	frm.findField('categoryId').setValue(newNode.id);
//							frm.findField('categoryId').setValue(newNode.id);
			            },  
			            afterchange: function(comboxtree,newNode,oldNode){//选择树结点设值之后，并当新值和旧值不相等时的事件  
			            }  
			     	}  
				},{
					xtype : 'hidden',
					name : 'orgId'
				},{
					xtype : 'combo',
					fieldLabel : '考核责任单位',
					hiddenName : 'checkOrgId',
					valueField : 'id',
					displayField : 'name',
					mode : 'remote',
					store : ds_org_select,
					selectOnFocus : true,
					editable : false,
					allowBlank : true,
					triggerAction : 'all',
					loadingText : '加载中...',
					emptyText : '请选择部门'
				}, {
					fieldLabel : '考核内容',
					name : 'content'
				}],
			buttonAlign : 'center',
			minButtonWidth : 60,
			buttons : [{
						text : '查询',
						handler : function(btn) {
							var frm = this.ownerCt.ownerCt.getForm();
							ds_query_goaldetail.baseParams = {
								year : frm.findField('year').getValue(),
								content : frm.findField('content').getValue(),
								categoryId : frm.findField('categoryId').getValue(),
								checkOrgId : frm.findField('checkOrgId').getValue(),
								status : 'audit_yes'
							};
							ds_query_goaldetail.load({params : {
								start : 0,limit : 20
							}});}
					},{
						text : '重置',
						handler : function() { 
							var frm = this.ownerCt.ownerCt.getForm();
							frm.findField('content').reset();
							frm.findField('checkOrgId').reset();
						}
					}]
		})]
	}]
});


//var btn_add_goalself_setting = new Ext.Button({
//	text : '加入',
//	iconCls : 'icon-add',
//	handler : function() {
//		if(!grid_query_goalself.checkMany()){
//			return;
//		} else {
//			Ext.Msg.confirm('确认提示', '你确定要加入这些考核指标?', function(btn) {
//				if (btn == 'yes') {
//				　 	var frm = Ext.getCmp('form_query_orgchecksetting').getForm();
//					var year = frm.findField('year').getValue();
//					var orgId = frm.findField('orgId').getValue();
//					var records = grid_query_goalself.getSelectionModel().getSelections();
//					var ids = [];
//					for(var i = 0, count = records.length; i < count; i++){
//						ids.push(records[i].get('id'));
//					}
//				//		提交保存数据
//					Ext.getBody().mask("数据正在提交，请稍等...","x-mask-loading"); 
//					Ext.Ajax.request({
//						url : '/supervise/biz/orgcheck/saveOrgCheckSetting.action',
//						success : function(response, options) {
//							Ext.getBody().unmask(); 
//							var store = grid_orgchecksetting.getStore();
//						    var fromRecord = null;
//							var toRecord = null;
//							for(var i = 0, count = records.length; i < count; i++){
//								toRecord = new OrgCheckSetting({
//					//						id : action.result.id,
//									detailId : records[i].get('id'),
//					//						categoryId : records[i].get('categoryId'),
//									content : records[i].get('content'),
//									point : records[i].get('point'),
//									spec : records[i].get('spec'),
//									orgId : records[i].get('checkOrgId'),
//									year : records[i].get('year'),
//									type : 'self'
//								});
//					//					alert(Ext.util.JSON.encode(toRecord));
//								store.insert(0, [toRecord]);
//							}
//							window_query_goalself.hide();
//						},
//						failure : function() {
//							Ext.getBody().unmask();
//							Ext.Msg.alert('提示', '数据保存失败！');
//						},
//						params : {
//							orgId : orgId,
//							goalIds : ids.join(','),
//							goalType : 'self'
//						}
//					});
//				}
//			});
//		}
//	}
//});
//var cm_query_goalself = new Ext.grid.ColumnModel([
//	new Ext.grid.CheckboxSelectionModel({singleSelect : false}),
//	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
//	{id: 'orgId',header: "申报部门", width: 100, hidden: true, dataIndex: 'orgId'},
//	{id: 'type',header: "指标类型", width: 80, sortable: true, dataIndex: 'type',renderer : fun_getGoalSelfType},
//	{id: 'content',header: "指标内容", width: 100, sortable: true, dataIndex: 'content'},
//	{id: 'spec',header: "评分标准", width: 100, sortable: true, dataIndex: 'spec'},
//	{id: 'point',header: "分值", width: 100, sortable: true, dataIndex: 'point'},
//	{id: 'ordering',header: "排序", width: 100, hidden: true, dataIndex: 'ordering'},
//	{id: 'memo',header: "备注", width: 100, hidden: true, dataIndex: 'memo'},
//	{id: 'checkOrgId',header: "考核责任单位", width: 100, sortable: true, dataIndex: 'checkOrgId',
//		renderer : function(v) {
//				var ret = '';
//				var size = ds_org_select.getCount();
//				for (var i = 0; i < size; i++) {
//					var record = ds_org_select.getAt(i);
//					var id = record.get('id');
//					if (id == v) {
//						ret = record.get('name');
//						break;
//					}
//				}
//				return (ret == '' || ret == null) ? v : ret;
//			}},
//    {id: 'year',header: "考核年度", width: 120, dataIndex: 'year'},
//	{id: 'auditManId',header: "审核人", width: 100, hidden: true, dataIndex: 'auditManId'},
//	{id: 'auditOrgId',header: "审核部门", width: 100, sortable: true, dataIndex: 'auditOrgId',
//		renderer : function(v) {
//				var ret = '';
//				var size = ds_org_select.getCount();
//				for (var i = 0; i < size; i++) {
//					var record = ds_org_select.getAt(i);
//					var id = record.get('id');
//					if (id == v) {
//						ret = record.get('name');
//						break;
//					}
//				}
//				return (ret == '' || ret == null) ? v : ret;
//			}},
//	{id: 'auditResult',header: "审核结果", width: 300, sortable: false, dataIndex: 'auditResult',renderer: fun_getAuditResult},
//    {id: 'auditContent',header: "审核意见", width: 80, sortable: false, dataIndex: 'auditContent'}
//]);
var ds_query_goalself = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/goalself/queryGoalSelfBySetting.action'
	}),
	reader : new Ext.data.JsonReader({
		root : 'root',
		totalProperty : 'totalProperty'
	}, [{
		name : 'id',
		type : 'int'
	}, {
		name : 'orgId',
		type : 'int'
	},{
		name : 'content',
		type : 'string'
	},{
		name : 'point',
		type : 'float'
	},{
		name : 'spec',
		type : 'string'
	}, {
		name : 'ordering',
		type : 'float'
	}, {
		name : 'memo',
		type : 'string'
	},{
		name : 'checkOrgId',
		type : 'int'
	},{
		name : 'year',
		type : 'int'
	},{
		name : 'auditManId',
		type : 'int'
	},{
		name : 'auditOrgId',
		type : 'int'
	},{
		name : 'auditResult',
		type : 'string'
	},{
		name : 'auditContent',
		type : 'string'
	},{
		name : 'type',
		type : 'string'
	}])
	});
//var grid_query_goalself = new Ext.grid.GridPanel({
//	loadMask : {
//		msg : '数据加载中...'
//	},
//	region : 'center',
//	height: 100,
////	autoHeight : true,
//	autoScroll : true, 
//	bodyStyle:'width:100%',
// 
////	viewConfig : {
////		forceFit : true
////	},
//	cm : cm_query_goalself,
//	sm : new Ext.grid.CheckboxSelectionModel({
//		singleSelect : false
//	}),
//	store : ds_query_goalself,
//	autoExpandColumn : 'content',
//	tbar : [btn_add_goalself_setting],
//	bbar : new Ext.PagingToolbar({
//				pageSize : 20,
//				store : ds_query_goaldetail,
//				displayInfo : true,
//				displayMsg : '第 {0} - {1} 条  共 {2} 条',
//				refreshText : "刷新",
//				emptyMsg : "没有记录"
//			}),
//	listeners : {
//		'render' : function(e) {
//			var frm = Ext.getCmp('form_query_orgchecksetting').getForm();
//			var year = frm.findField('year').getValue();
//			var orgId = frm.findField('orgId').getValue();
//			ds_query_goalself.load({params : {
//				start : 0,limit : 20,
//				year : year,
//				orgId : orgId
//			}});
//			grid_query_goalself.render();
//		}
//	},
//	// 检测至少选择一个
//	checkOne : function() {
//		var selections = this.getSelectionModel().getSelections();
//		if (selections.length == 0) {
//			Ext.MessageBox.alert("提示", "请选择一条的记录！");
//			return false;
//		} else if (selections.length != 1) {
//			Ext.MessageBox.alert("提示", "不能选择多行！");
//			return false;
//		}
//		return true;
//	},
//
//	// 检测必须选择一个
//	checkMany : function() {
//		var selections = this.getSelectionModel().getSelections();
//		if (selections.length == 0) {
//			Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
//			return false;
//		}
//		return true;
//	}
//});
